﻿using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using SDG.UnitTesting.Remoting;

namespace SDG.UnitTesting
{
    [AttributeUsage(AttributeTargets.Class)]
    public class SDGAttribute : ContextAttribute
    {
        private const string _Name = "Red Gate SDG";
        private string _exe;

        public SDGAttribute() : this(Properties.Settings.Default.DefaultConsoleExe)
        {}

        public SDGAttribute(string exe) : base(_Name)
	    {
            _exe = exe;
        }

        //ContextAttribute Methods

        public override void Freeze(Context newContext)
        {
            //Do nothing
        }

        public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
        {
            ctorMsg.ContextProperties.Add(new SDGProperty(_exe));
        }

        public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)
        {
            SDGProperty property = ctx.GetProperty(_Name) as SDGProperty;
            if (property == null)
                return false;

            return true;
        }

        public override bool IsNewContextOK(Context newCtx)
        {
            SDGProperty property = newCtx.GetProperty(_Name) as SDGProperty;
            if (property == null)
                return false;

            return true;
        }
    }
}
